package com.ruoyi.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.system.domain.ReceiptForm;
import com.ruoyi.system.service.IReceiptFormService;

/**
 * 料单Controller
 * 
 * @author ruoyi
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/labbase/receipt_form")
public class ReceiptFormController extends BaseController {
	@Autowired
	private IReceiptFormService receiptFormService;
	@Autowired
	private ILabOrgnizationService labOrgnizationService;
	/**
	 * 查询料单列表
	 */
	@PreAuthorize("@ss.hasPermi('labbase:receipt_form:query')")
	@GetMapping("/list")
	public TableDataInfo list(ReceiptForm receiptForm) {
		startPage();
		List<ReceiptForm> list = receiptFormService.selectReceiptFormList(receiptForm);
		return getDataTable(list);
	}
	
	/*@PreAuthorize("@ss.hasPermi('labbase:receipt_form:query')")
	@GetMapping("/query")
	public TableDataInfo query(ReceiptForm receiptForm) {
		startPage();
		List<ReceiptForm> list = receiptFormService.queryProductInventory(receiptForm);
		return getDataTable(list);
	}*/

	/**
	 * 导出料单列表
	 */
	// @PreAuthorize("@ss.hasPermi('labbase:receipt_form:export')")
	/*@Log(title = "料单", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(ReceiptForm receiptForm) {
		List<ReceiptForm> list = receiptFormService.selectReceiptFormList(receiptForm);
		ExcelUtil<ReceiptForm> util = new ExcelUtil<ReceiptForm>(ReceiptForm.class);
		return util.exportExcel(list, "receipt_form");
	}*/

	/**
	 * 获取料单详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labbase:receipt_form:query')")
	@GetMapping(value = "/{receiptId}")
	public AjaxResult getInfo(@PathVariable("receiptId") Long receiptId) {
		ReceiptForm receiptForm = receiptFormService.selectReceiptFormById(receiptId);
		//获取机构名称
		if(receiptForm.getCollegeOrgId()!=null){
			LabOrgnization org = labOrgnizationService.selectLabOrgnizationById(receiptForm.getCollegeOrgId());
			receiptForm.setCollegeOrgName(org.getOrgName());
		}
		if(receiptForm.getLabId()!=null){
			LabOrgnization lab = labOrgnizationService.selectLabOrgnizationById(receiptForm.getLabId());
			receiptForm.setLabName(lab.getOrgName());
		}
		return AjaxResult.success(receiptForm);
	}

	/**
	 * 新增料单
	 */
	@PreAuthorize("@ss.hasPermi('labbase:receipt_form:edit')")
	@Log(title = "料单", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody ReceiptForm receiptForm) {
		 receiptFormService.insertReceiptForm(receiptForm);
		 //receiptForm.setImages(receiptFormService.getInstanceFiles(receiptForm.getInstanceId()));
		 return AjaxResult.success(receiptForm);
	}

	/**
	 * 修改料单
	 */
	@PreAuthorize("@ss.hasPermi('labbase:receipt_form:edit')")
	@Log(title = "料单", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody ReceiptForm receiptForm) {
		receiptFormService.updateReceiptForm(receiptForm);
		//receiptForm.setImages(receiptFormService.getInstanceFiles(receiptForm.getInstanceId()));
		return AjaxResult.success(receiptForm);
	}
	@PreAuthorize("@ss.hasPermi('labbase:receipt_form:edit')")
	@Log(title = "提交料单流程", businessType = BusinessType.UPDATE)
	@PostMapping("/commit")
	public AjaxResult commit(@RequestBody ReceiptForm receiptForm) {
		 return AjaxResult.success( receiptFormService.commitReceiptForm(receiptForm));
	}
	/**
	 * 删除料单
	 */
	@PreAuthorize("@ss.hasPermi('labbase:receipt_form:edit')")
	@Log(title = "料单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiptIds}")
	public AjaxResult remove(@PathVariable Long[] receiptIds) {
		return toAjax(receiptFormService.deleteReceiptFormByIds(receiptIds));
	}
}
